package com.example.asus.a222dzd_ordering.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.asus.a222dzd_ordering.R;
import com.example.asus.a222dzd_ordering.activity.SearchResultActivity;
import com.example.asus.a222dzd_ordering.base.BaseListner;
import com.example.asus.a222dzd_ordering.base.BaseModel;
import com.example.asus.a222dzd_ordering.bean.FoodListBean;


import java.io.Serializable;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFragment extends Fragment{
    private EditText et_search;
    private Button bt_search;
    private List<FoodListBean> beans;

    public SearchFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_search, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData(view);
        initEvent(view);
    }

    private void initView(View v){
        et_search = v.findViewById(R.id.et_search);
        bt_search = v.findViewById(R.id.bt_search);
    }

    private void initData(View v){

    }

    private void initEvent(View v){
        bt_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchText = et_search.getText().toString();
                BaseModel<List<FoodListBean>> model = new BaseModel<>();
                model.callEnqueue(model.service.getSearch(searchText),listner);

            }
        });
    }

    BaseListner<List<FoodListBean>> listner = new BaseListner<List<FoodListBean>>() {
        @Override
        public void onResponse(List<FoodListBean> foodListBeans) {
            beans=foodListBeans;
            if (beans==null)
                Toast.makeText(getContext(),"Kong", Toast.LENGTH_LONG).show();
            else{
                Intent intent = new Intent(getContext(),SearchResultActivity.class);
                intent.putExtra("beanlist",(Serializable) beans);
                startActivity(intent);

            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(),msg, Toast.LENGTH_SHORT).show();
        }
    };
}
